var ctxPath = "/education";

$.getUrlParam = function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
};

!function ($) {
    var $tab = $('#J_Tab');

    $tab.find('.tab-nav-item').on('opened.ydui.tab', function (e) {
        console.log('索引：%s - [%s]已经打开了', e.index, $(this).text());
    });

    var activeTab = $.getUrlParam('tab');
    if(null != activeTab && "" != activeTab){
        $("#" + activeTab).addClass("tab-active");
        $("." + activeTab).addClass("tab-active");
    }else{
        $("#all-tab").addClass("tab-active");
        $(".all-tab").addClass("tab-active");
    }

    var initOrders = function() {
        $.ajax({
            url: ctxPath + '/wx/api/order/queryOrders',
            type: 'post',
            dataType: 'json',
            success: function (result) {
                if(result.returnCode == 0) {

                    var alls = result.alls;
                    var unPays = result.unPays;
                    var applies = result.applies;
                    var pays = result.pays;
                    var completes = result.completes;
                    if(alls.length > 0){
                        $(".all-tab").html("");

                        $(alls).each(function(index, ele){
                            $(".all-tab").append("<div class=\"pro-box pay-num\"><span>订单编号:" + ele.orderCode + "</span><span class=\"floatr text-red\">" + ele.orderStatus + "</span></div>");
                            $(".all-tab").append("<article class=\"m-list list-theme4 pay-list\"><a href='"+ "/education/wx/orderDetail.html?orderCode=" + ele.orderCode + "' class=\"list-item\"><div class=\"list-img\"><img src='" + ele.orderImage + "'></div>" +
                                "<div class=\"list-mes\"><div class=\"list-info\"><h3 class=\"list-title\">" + ele.productName + "</h3>" +
                                "<p>" + ele.groupName + "</p>" + "<p>" + ele.phone + "</p>" + "<p>" + ele.schoolGradeClazz + "</p></div>" +
                                "<div class=\"list-mes-item\"><span class=\"list-price\"><em>¥</em>" + ele.productDeposit + "</span></div>" +
                                "</div></a></article>");
                        });

                        if(unPays.length > 0){
                            $(".un-pay-tab").html("");

                            $(unPays).each(function(index, ele){
                                $(".un-pay-tab").append("<div class=\"pro-box pay-num\"><span>订单编号:" + ele.orderCode + "</span><span class=\"floatr text-red\">" + ele.orderStatus + "</span></div>");
                                $(".un-pay-tab").append("<article class=\"m-list list-theme4 pay-list\"><a href='"+ "/education/wx/orderDetail.html?orderCode=" + ele.orderCode + "' class=\"list-item\"><div class=\"list-img\"><img src='" + ele.orderImage + "'></div>" +
                                    "<div class=\"list-mes\"><div class=\"list-info\"><h3 class=\"list-title\">" + ele.productName + "</h3>" +
                                    "<p>" + ele.groupName + "</p>" + "<p>" + ele.phone + "</p>" + "<p>" + ele.schoolGradeClazz + "</p></div>" +
                                    "<div class=\"list-mes-item\"><span class=\"list-price\"><em>¥</em>" + ele.productDeposit + "</span></div>" +
                                    "</div></a></article>");
                            });
                        }

                        if(applies.length > 0){
                            $(".applies-tab").html("");

                            $(applies).each(function(index, ele){
                                $(".applies-tab").append("<div class=\"pro-box pay-num\"><span>订单编号:" + ele.orderCode + "</span><span class=\"floatr text-red\">" + ele.orderStatus + "</span></div>");
                                $(".applies-tab").append("<article class=\"m-list list-theme4 pay-list\"><a href='"+ "/education/wx/orderDetail.html?orderCode=" + ele.orderCode + "' class=\"list-item\"><div class=\"list-img\"><img src='" + ele.orderImage + "'></div>" +
                                    "<div class=\"list-mes\"><div class=\"list-info\"><h3 class=\"list-title\">" + ele.productName + "</h3>" +
                                    "<p>" + ele.groupName + "</p>" + "<p>" + ele.phone + "</p>" + "<p>" + ele.schoolGradeClazz + "</p></div>" +
                                    "<div class=\"list-mes-item\"><span class=\"list-price\"><em>¥</em>" + ele.productDeposit + "</span></div>" +
                                    "</div></a></article>");
                            });
                        }

                        if(pays.length > 0){
                            $(".pays-tab").html("");

                            $(pays).each(function(index, ele){
                                $(".pays-tab").append("<div class=\"pro-box pay-num\"><span>订单编号:" + ele.orderCode + "</span><span class=\"floatr text-red\">" + ele.orderStatus + "</span></div>");
                                $(".pays-tab").append("<article class=\"m-list list-theme4 pay-list\"><a href='"+ "/education/wx/orderDetail.html?orderCode=" + ele.orderCode + "' class=\"list-item\"><div class=\"list-img\"><img src='" + ele.orderImage + "'></div>" +
                                    "<div class=\"list-mes\"><div class=\"list-info\"><h3 class=\"list-title\">" + ele.productName + "</h3>" +
                                    "<p>" + ele.groupName + "</p>" + "<p>" + ele.phone + "</p>" + "<p>" + ele.schoolGradeClazz + "</p></div>" +
                                    "<div class=\"list-mes-item\"><span class=\"list-price\"><em>¥</em>" + ele.productDeposit + "</span></div>" +
                                    "</div></a></article>");
                            });
                        }

                        if(completes.length > 0){
                            $(".complete-tab").html("");

                            $(completes).each(function(index, ele){
                                $(".complete-tab").append("<div class=\"pro-box pay-num\"><span>订单编号:" + ele.orderCode + "</span><span class=\"floatr text-red\">" + ele.orderStatus + "</span></div>");
                                $(".complete-tab").append("<article class=\"m-list list-theme4 pay-list\"><a href='"+ "/education/wx/orderDetail.html?orderCode=" + ele.orderCode + "' class=\"list-item\"><div class=\"list-img\"><img src='" + ele.orderImage + "'></div>" +
                                    "<div class=\"list-mes\"><div class=\"list-info\"><h3 class=\"list-title\">" + ele.productName + "</h3>" +
                                    "<p>" + ele.groupName + "</p>" + "<p>" + ele.phone + "</p>" + "<p>" + ele.schoolGradeClazz + "</p></div>" +
                                    "<div class=\"list-mes-item\"><span class=\"list-price\"><em>¥</em>" + ele.productDeposit + "</span></div>" +
                                    "</div></a></article>");
                            });
                        }
                    }
                } else {
                    alert(result.returnMsg);
                    window.location.href = "/education/wx/customerLogin.html?redirect=/education/wx/orders.html";
                }
            }
        });
    };

    initOrders();

}(jQuery);